package com.zcaxer.way.data

import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.google.gson.annotations.Expose


@Entity(tableName = "Building_table")
data class Building(
        @PrimaryKey @Expose var ID: Int,
        @Expose var name: String,
        @Expose var foodIncrease: Int,
        @Expose var maxWorkersA: Float,
        @Expose var maxWorkersB: Int,
        @Expose @Ignore var upgradeInfo: List<Binomial> = emptyList(),
        var level: Int = 1,
        var reserve: Int = 0,
        var workers: Int = 0,
        var maxWorkers: Int = 0,
        var capacity: Int = 0,
        var upWoodNeed: Int = 1,
        var workersNeed: Int = 0,
        var target: Int = 0

) {


    constructor(BuildingID: Int, buildingName: String, foodIncrease: Int, maxWorkersA: Float, maxWorkersB: Int)
            : this(BuildingID, buildingName, foodIncrease, maxWorkersA, maxWorkersB, emptyList()) {
    }

    constructor() : this(-1, "null", 0, -1f, -1)


    companion object {

        fun calculate(type: Int, infoList: List<Binomial>, l: Int): Int {
            var x = 0
            infoList.let {
                val iterator = it.iterator()
                while (iterator.hasNext()) {
                    val info = iterator.next()
                    if (type == info.type) {
                        if (l < info.endLevel)
                            return (info.c + l * info.b + l * l * info.a).toInt()
                    }
                }
            }
            return x
        }

    }

    fun initBuilding(): Building {

        if (level <= 0) {
            level = 1
        }
        maxWorkers = (maxWorkersA * level + maxWorkersB).toInt()
        capacity = calculate(Constant.CAPACITY, upgradeInfo, level)
        upWoodNeed = calculate(Constant.UPWOODNEED, upgradeInfo, level)

        return this
    }


}





